﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Guide
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
            doDataBound();
            //var control = (Stock)LoadControl("Stock.ascx");
            //control.LabelContext = "dasssssssssss";
            //control.ListProvince = (List<Province>)this.GridView1.DataSource;
            //Hello.Controls.Add(control);
            //}
        }

        private void doDataBound()
        {
            this.GridView1.DataSource = Province.getProvice();
            //this.GridView1.ViewStateMode=System.Web.UI.ViewStateMode.Enabled;
            this.GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            GridView1.AllowPaging = false;
            GridView1.DataBind();
            foreach (GridViewRow row in this.GridView1.Rows)
            {
                var temp = row.FindControl("ProvinceCName");
                Label label = row.FindControl("ProvinceCName") as Label;
                sb.Append(label.Text);
            }
            var dataSource = this.GridView1.DataSource;
            var x = this.SqlDataSource1;
            sb.Append(dataSource.ToString());
            //this.Label1.Text = sb.ToString();
            GridView1.AllowPaging = true;
            GridView1.DataBind();
            Session["listprovince"] = dataSource;
            Response.Redirect("/Select1.aspx");
            //window.execScript("report_back('Printing complete!')", "JScript");
            //Page.RegisterClientScriptBlock( ,"Hello", "alert('Hello');");
            //Page.RegisterStartupScript("Hello", "<script type='text/javascript'>alert('Hello');</script>");
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            DisplayCurrentPage();
        }

        void DisplayCurrentPage()
        {
            // Calculate the current page number.
            // int currentPage = CustomersGridView.PageIndex + 1;

            // Display the current page number. 
            // Message.Text = "Page " + currentPage.ToString() + " of " +
            //  CustomersGridView.PageCount.ToString() + ".";
        }


        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            // Cancel the paging operation if the user attempts to navigate
            // to another page while the GridView control is in edit mode. 
            if (GridView1.EditIndex != -1)
            {
                // Use the Cancel property to cancel the paging operation.
                e.Cancel = true;

                // Display an error message.
                int newPageNumber = e.NewPageIndex + 1;

                GridView1.PageIndex = e.NewPageIndex;
                doDataBound();
                //Message.Text = "Please update the record before moving to page " +
                //  newPageNumber.ToString() + ".";
            }
            else
            {
                // Clear the error message.
                //Message.Text = "";
            }

            GridView1.PageIndex = e.NewPageIndex;
            doDataBound();

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            GridView1.AllowPaging = false;
            //GridView1.DataBind();
            foreach (GridViewRow row in this.GridView1.Rows)
            {
                bool isChecked = false;
                if ((row.FindControl("CheckBox1") as CheckBox).Checked == true)
                {
                    isChecked = true;
                }
                else
                {
                    isChecked = false;
                }

                if (isChecked)
                {
                    var temp = row.FindControl("ProvinceCName");
                    Label label = row.FindControl("ProvinceCName") as Label;
                    sb.Append(label.Text);
                }
            }
            var dataSource = this.GridView1.DataSource;
            var x = this.SqlDataSource1;
            //sb.Append(dataSource.ToString());
            string result = sb.ToString();
            this.Label1.Text = sb.ToString();
            GridView1.AllowPaging = true;
            GridView1.DataBind();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            var dataSource = this.GridView1.DataSource;
            Session["listprovince"] = dataSource;
            Response.Redirect("/Printer.aspx");
        }

        //protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        //{
        //    if (e.Row.RowType == DataControlRowType.DataRow)
        //    {
        //        var province = Province.getProvice(); 
        //        if (material != null)
        //        {
        //            var lblMaterialName = e.Row.FindControl(lblMaterialNameID) as Label;
        //            lblMaterialName.Text = material.Name;
        //            var lblLabOffice = e.Row.FindControl(lblLabOfficeID) as Label;
        //            lblLabOffice.Text = material.LabOffice;
        //            var lblFamily = e.Row.FindControl(lblFamilyID) as Label;
        //            lblFamily.Text = material.Family;
        //            var lblMinUnitPackage = e.Row.FindControl(lblMinUnitPackageID) as Label;
        //            lblMinUnitPackage.Text = string.Format("{0:N0}", material.MinUnitPackage);
        //        }
        //    }
        //}

    }
}
